package com.jf.cloud.service.sysmanager.remote.api;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jf.cloud.service.sysmanager.common.util.StringUtil;
import com.jf.cloud.service.sysmanager.common.util.qnImg.UplodImg;
import com.jf.cloud.service.sysmanager.constant.ImagePath;
import com.jf.cloud.service.sysmanager.dao.IPubgUserService;
import com.jf.cloud.service.sysmanager.handler.TokenHandler;
import com.jf.cloud.service.sysmanager.support.entity.Result;

/**
 * 上传图片
 * 
 * @author 朱建文 创建时间：2017年12月14日 下午2:22:27
 */
@Controller
@RequestMapping("/appUploadImg")
public class UploadImgController {

	/**
	 * 日志
	 */
	private static Log log = LogFactory.getLog(UploadImgController.class);

	/**
	 * 员工服务
	 */
	@Resource
	private IPubgUserService pubgUserService;
	
	@Resource
	private TokenHandler tokenHandler;
	
	/**
	 * 上传图片
	 * 
	 * @author 朱建文 创建时间：2018年4月10日 下午4:36:03
	 */
	@ResponseBody
	@RequestMapping("/upload")
	public Result<String> uploadImg(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		Result<String> result = new Result<String>();
		result.setCode(-1);
		result.setMessage("文件上传失败！");
		try {
			
			String token = request.getParameter("token");
			String userId = tokenHandler.getCommUserInfoByToken(token);
			if (userId == null || userId.trim().length() == 0) {
				result.setCode(-2);
				return result;
			}
			
			String imgType = request.getParameter("imgType");
			if (!ImagePath.T_USER_PHOTO.equals(imgType) 
					&& !ImagePath.T_TEAM_PHOTO.equals(imgType)
					&& !ImagePath.T_INFORM_PHOTO.equals(imgType)
					&& !ImagePath.T_REAL_NAME_IMG.equals(imgType)) {
				result.setMessage("图片类型不合法！");
				return result;
			}
			
			String imageId = request.getParameter("imageId");
            if(StringUtil.isEmpty(imageId)){
            	result.setMessage("imageId为空！");
                return result;
            }
            
			String	path = UplodImg.up(request, imgType, imageId, response);
			if (StringUtil.isBlank(path)) {
				result.setMessage("请选择上传图片！");
				return result;
			}
			
			if (path.equals("isNotImg")) {
				result.setMessage("上传文件只能为图片！");
				return result;
			} else if (path.equals("bigger")) {
				result.setMessage("图片文件太大不能上传！");
				return result;
			} else {
				result.setData(path);
			}
			
			// 用户头像需要更新 用户表
			if (ImagePath.T_USER_PHOTO.equals(imgType)) {
				pubgUserService.updateUserPhoto(Long.valueOf(userId), path);
			}
			result.setCode(0);
			result.setMessage("上传图片成功！");
		} catch (Exception e) {
			log.error("文件上传失败。", e);
		}
		return result;
	}
	
}
